﻿Imports Microsoft.VisualBasic
Imports System.ComponentModel

Public Class Helper
    Implements IDisposable
    Dim bd As New bdSeguridadDataContext()
#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: dispose managed state (managed objects).
                bd.Dispose()
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region


    Function GetUsuario(ByVal usuario As String) As Web_Usuario
        Using dbsec As New bdSeguridadDataContext
            Dim usr = From u In dbsec.Web_Usuarios
                       Select u
                       Where u.Usuario = usuario

            Return CType(usr.FirstOrDefault, Web_Usuario)
        End Using
    End Function


    Public Sub verficarSeguridad(ByVal usuario As String, ByVal codModulo As Integer, ByRef page As Page)
        'Dim bd As New bdSeguridadDataContext()
        Dim permiso = bd.Web_SP_GetPermisoModulo(usuario, codModulo)
        If permiso.First.Permiso = 0 Then
            page.Session.Abandon()
            FormsAuthentication.SignOut()
            FormsAuthentication.RedirectToLoginPage()
        End If
    End Sub
    Public Sub verficarSeguridadSubmodulo(ByVal usuario As String, ByVal codsubModulo As Integer, ByRef page As Page)
        'Dim bd As New bdSeguridadDataContext()
        Dim permiso = bd.Web_SP_GetPermisoSubmodulo(usuario, codsubModulo)
        If permiso.First.Permiso = 0 Then
            page.Session.Abandon()
            FormsAuthentication.SignOut()
            FormsAuthentication.RedirectToLoginPage()
        End If
    End Sub
    Public Function VerificarPermiso(ByVal usuario As String, ByVal codsubmodulo As Integer, ByVal codpermiso As permisos) As Boolean
        Dim permiso = bd.Web_SP_GetPermiso(usuario, codsubmodulo, codpermiso)
        Dim result As Boolean = False
        If permiso.First.Permiso = 1 Then
            result = True
        End If
        Return result
    End Function

    Public Sub InsertarAuditoria(ByVal usuario As String, ByVal idSubmodulo As Integer, ByVal pagina As String, ByVal detalle As String)
        Dim a As New web_Auditoria()
        Dim u As New Web_Usuario()
        u = GetUsuario(usuario)
        a.idUsuario = u.IdUsurio
        a.idSubModulo = idSubmodulo
        a.Pagina = pagina
        a.detalle = detalle
        'web_AuditoriaInsert(usuario, idSubmodulo, pagina, detalle)
        bd.web_Auditorias.InsertOnSubmit(a)
        bd.SubmitChanges()
    End Sub


    Public Sub InsertarError(ByVal usuario As String, ByVal idSubmodulo As Integer, ByVal pagina As String, ByVal erro As String)
        Dim a As New web_Errore()
        Dim u As New Web_Usuario()
        u = GetUsuario(usuario)
        a.idUsuario = u.IdUsurio
        a.idSubModulo = idSubmodulo
        a.Pagina = pagina
        a.error = erro
        a.FechaRegistro = Now
        bd.web_Errores.InsertOnSubmit(a)
        bd.SubmitChanges()
    End Sub

    Enum permisos
        Insertar = 1
        Editar = 2
        Eliminar = 3
        Consultar = 4
    End Enum

    Shared Function TextotoBD(ByVal texto As String) As String
        If String.IsNullOrEmpty(texto) Then
            Return Nothing
        Else
            Return texto
        End If
    End Function
    Shared Function DatetoBD(ByVal texto As String) As Nullable(Of Date)
        If IsDate(texto) Then
            Return CDate(texto)
        Else
            Return Nothing
        End If
    End Function
    Shared Function BDtoDate(ByVal texto As Nullable(Of Date)) As String
        If texto.HasValue Then
            Return CDate(texto).ToShortDateString
        Else
            Return ""
        End If
    End Function

End Class
